<template>
  <div>
    <el-button type="primary" icon="el-icon-circle-plus" @click="add()">添加通知</el-button>
    <el-table
      :data="notice"
      stripe
      border
      style="width: 100%;margin-top: 10px">
      <el-table-column
        label="id"
        align="center"
        prop="id">
      </el-table-column>
      <el-table-column
        label="标题"
        align="center"
        prop="title">
      </el-table-column>
      <el-table-column
        label="外部链接"
        align="center"
        prop="url">
      </el-table-column>
      <el-table-column
        label="发布时间"
        :formatter="dateFormat"
        align="center"
        prop="createTime">
      </el-table-column>
      <el-table-column
        align="center"
        width="300px"
        label="操作">
        <template slot-scope="scope">
          <el-button round
                     @click="handleCheck(scope.$index, scope.row)" plain size="mini">详 情
          </el-button>
          <el-button round
                     @click="qianCheck(scope.$index, scope.row)" plain size="mini">签收详情
          </el-button>
          <el-button round
                     type="danger"
                     @click="handleDelete(scope.$index, scope.row)" plain size="mini">删 除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin: 10px"
      background
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
      :total="total">
    </el-pagination>

    <el-dialog title="签收情况" @close='closeDialog' :visible.sync="showQianMsg" width="800px" top="20px" >
      <el-table
        :data="qianList"
        stripe
        border
        style="width: 100%;margin-top: 10px">
        <el-table-column
          label="姓名"
          align="center"
          prop="name">
        </el-table-column>
        <el-table-column
          label="身份证"
          align="center"
          prop="identityCard">
        </el-table-column>
        <el-table-column
          label="是否签收"
          align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.isQian == true" style="color: red">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>

      </el-table>

      <el-pagination
        style="margin: 10px"
        background
        layout="prev, pager, next"
        @current-change="handleCurrentChange1"
        :total="total1">
      </el-pagination>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showQianMsg = false">取 消</el-button>
        <el-button type="primary" @click="showQianMsg = false">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" @close='closeDialog' :visible.sync="showNoticeMsg" width="800px" top="20px" >
      <div style="height: 500px;overflow-y: auto;overflow-x: hidden;">
        <el-form
          size="mini"
          :model="itemMsg" label-width="80px"
          style="margin: 20px">
          <el-form-item label="标题:">
            <el-input  v-model="itemMsg.title"></el-input>
          </el-form-item>
          <el-form-item label="外部链接:">
            <el-input  v-model="itemMsg.url"></el-input>
          </el-form-item>
        </el-form>
        <vue-neditor-wrap v-model="itemMsg.content" :config="myConfig" :destroy="false" @ready="ready" style="margin-right: 20px;margin-left: 20px"></vue-neditor-wrap>
        <el-upload
          class="upload-demo"
          :action="CONFIG.serverSrc+'/file/upload'"
          name="file"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-success="success"
          :before-remove="beforeRemove"
          :limit="5"
          :before-upload="onBeforeUpload"
          :on-exceed="handleExceed"
          style="margin-top: 10px;margin-left: 20px"
          :file-list="itemMsg.files">
          <el-button size="small" type="primary">点击上传附件</el-button>
          <div slot="tip" class="el-upload__tip">最多只能上传100M以内</div>
        </el-upload>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showNoticeMsg = false">取 消</el-button>
        <el-button type="primary" @click="updateMsg()" v-if="type == 0">修 改</el-button>
        <el-button type="primary" @click="addMsg()" v-else>增 加</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
    import moment from 'moment'
    import VueNeditorWrap from "vue-neditor-wrap";
    import {noticeList,noticeSave,noticeUpdate,noticeDelete,findQian} from '../../axios/api'
    export default {
        name: "notice",
        components:{
            VueNeditorWrap
        },
        data(){
            return{
                showQianMsg:false,
                showNoticeMsg:false,
                title:"修改通知",
                type:0,//0修改 1增加
                total:0,
                total1:0,
                myConfig: {
                    // 如果需要上传功能,找后端小伙伴要服务器接口地址
                    serverUrl: this.CONFIG.serverSrc+'/file/upload',
                    // 你的UEditor资源存放的路径,相对于打包后的index.html
                    UEDITOR_HOME_URL: '/static/NEditor/',
                    // 编辑器不自动被内容撑高
                    autoHeightEnabled: false,
                    // 初始容器高度
                    initialFrameHeight: 240,
                    // 初始容器宽度
                    initialFrameWidth: '100%',
                    // 关闭自动保存
                    enableAutoSave: false
                },
                itemMsg:{
                    id:0,
                    title:"",
                    content:"",
                    url:"",
                    files:[]
                },
                notice:[],
                page:1,
                qianList:[],
                id:0
            }
        },
        mounted() {
            this.getMSG(1,10);
        },
        methods:{
            qianCheck(index,row){
                this.id = row.id;
                this.findQianMsg(row.id)
            },
            findQianMsg:async function(id,page,limit){
                let data = {
                    id:id,
                    page:page,
                    limit:limit,
                    is_admin:false
                }
                let res = await findQian(data);
                if (res.code === 0){
                    this.qianList = res.data.list;
                    this.total1 = res.data.totalCount;
                    this.showQianMsg = true;
                }
            },
            getMSG:async function(page,limit){
                let data = {
                    page:page,
                    limit:limit
                };
                let res = await noticeList(data);
                if (res.code === 0){
                    let notices = [];
                    res.page.list.forEach(function (data,i,a) {
                        notices.push({
                            id:data.id,
                            title:data.title,
                            url:data.url,
                            content:data.content,
                            files:eval('(' +data.files+ ')'),
                            createTime:data.createTime
                        })
                    });
                    this.notice = notices;
                    this.total = res.page.totalCount;
                }
            },
            onBeforeUpload(file)
            {
                const isLt1M = file.size / 1024 / 1024 < 1;

                if (!isLt1M) {
                    this.$message.error('上传文件大小不能超过 1MB!');
                }
                return isLt1M;
            },
            handleDelete(index,row){
                this.$confirm('此操作将永久删除该通知, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteMsg(row.id);
                }).catch(() => {

                });
            },
            deleteMsg:async function(id){
                let data = {id:id};
                let res = await noticeDelete(data);
                if (res.code === 0){
                    this.alert("success",'删除成功!');
                    this.getMSG(this.page,10);
                }
            },
            success(res,file){
                if (res.code === 200){
                    this.alert("success",'上传成功!');
                    this.itemMsg.files.push({
                        name:file.name,
                        url:this.CONFIG.serverSrc + '/file/' + res.url
                    })
                }else{
                    this.alert("error",'上传失败!');
                }

            },
            handleCurrentChange(index){
                this.page = index;
                this.getMSG(index,10);
            },
            handleCurrentChange1(index){
                this.findQianMsg(this.id,index,10)
            },
            ready (editorInstance) {
                console.log(`编辑器实例${editorInstance.key}: `, editorInstance)
            },
            dateFormat:function(row, column) {
                var date = row.createTime;
                if (date == undefined) {
                    return "";
                }
                return moment(parseInt(date)).format("YYYY-MM-DD");
            },
            add(){
              this.title = "添加通知";
              this.type = 1;
              this.showNoticeMsg = true;
            },
            closeDialog(){
                this.itemMsg = {
                    id:0,
                    title:"",
                    content:"",
                    url:"",
                    files:[]
                }
            },
            handleCheck(index,row){
                this.title = "通知详情";
                this.type = 0;
                this.showNoticeMsg = true;
                this.itemMsg = row
            },
            addMsg:async function(){
                if (this.itemMsg.title === ""){
                    this.alert("warning","请填写标题！");
                    return;
                }
                if (this.itemMsg.url === "" && this.itemMsg.content === ""){
                    this.alert("warning","标题与内容必须填写一个！");
                    return;
                }
                let data = {
                    title:this.itemMsg.title,
                    content:this.itemMsg.content,
                    url:this.itemMsg.url,
                    files:JSON.stringify(this.itemMsg.files)
                };
                let res = await noticeSave(data);
                if (res.code === 0){
                    this.alert("success","添加成功");
                    this.getMSG(this.page,10);
                }
            },
            updateMsg:async function(){
                if (this.itemMsg.title === ""){
                    this.alert("warning","请填写标题！");
                    return;
                }
                if (this.itemMsg.url === "" && this.itemMsg.content === ""){
                    this.alert("warning","标题与内容必须填写一个！");
                    return;
                }
                let data = {
                    id:this.itemMsg.id,
                    title:this.itemMsg.title,
                    content:this.itemMsg.content,
                    url:this.itemMsg.url,
                    files:JSON.stringify(this.itemMsg.files)
                };
                let res = await noticeUpdate(data);
                if (res.code === 0){
                    this.alert("success","修改成功");
                    this.getMSG(this.page,10)
                }
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePreview(file) {
                console.log(file);
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            beforeRemove(file, fileList) {
                let than = this;
                this.$confirm(`确定移除 ${ file.name }？`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.itemMsg.files.forEach(function (data,i,a) {
                        if (data.name === file.name){
                            than.itemMsg.files.splice(i, 1);
                            return true
                        }
                    })
                }).catch(() => {
                    return false
                });
                return false
            }
        }
    }
</script>

<style scoped>

</style>
